package com.db.collections.memory;


import com.db.collections.inthashmap.IntHashMap;

import java.util.*;

public class Memory {
    public static final int SIZE = 100000;

    public static void main(String[] args) {

        int i = 0;
        long start = getUsedMemory();
        int[] array = new int[SIZE];
        while (i < SIZE) {
            array[i] = i;
            i++;
        }
        long finish = getUsedMemory();
        System.out.println("array " + (finish - start) / SIZE);


        i = 0;
        start = getUsedMemory();
        List<Integer> arrayList = new ArrayList<>(array.length);
        while (i < SIZE) {
            arrayList.add(i, i);
            i++;
        }
        finish = getUsedMemory();
        System.out.println("array list " + (finish - start) / SIZE);


        i = 0;
        start = getUsedMemory();
        List<Integer> linkedList = new LinkedList<>();
        while (i < SIZE) {
            linkedList.add(i);
            i++;
        }
        finish = getUsedMemory();
        System.out.println("linked list " + (finish - start) / SIZE);

        i = 0;
        Integer key;
        start = getUsedMemory();
        Map<Integer, Integer> hashMap = new HashMap<>();
        while (i < SIZE) {
            key = Integer.valueOf(i+128);
            hashMap.put(key, key);
            i++;
        }
        finish = getUsedMemory();
        System.out.println("hashMap " + (finish - start) / SIZE);

        i = 0;
        start = getUsedMemory();
        Map<Integer, Integer> treeMap = new TreeMap<>();
        while (i < SIZE) {
            key = Integer.valueOf(i+128);
            treeMap.put(key, key);
            i++;
        }
        finish = getUsedMemory();
        System.out.println("treeMap " + (finish - start) / SIZE);


        i = 0;
        start = getUsedMemory();
        IntHashMap intHashMap = new IntHashMap();
        while (i < SIZE) {
            intHashMap.put(i, i);
            i++;
        }
        finish = getUsedMemory();
        System.out.println("intHashMap " + (finish - start) / SIZE);

        intHashMap.contains(4);
        if (array.length > 0);
        hashMap.size();
        arrayList.size();
        linkedList.size();
        double avg = 0.;

        for (int num : array) {
            avg += num;
        }
    }

    private static long getUsedMemory() {
        return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
    }
}

/*
array 4
array list 20
linked list 39
hashMap 56
treeMap 51
*/